//
//  TimeSetView.m
//  IntelligenceCommunity
//
//  Created by zhangpei on 15/12/15.
//  Copyright © 2015年 zhangpei. All rights reserved.
//

#import "TimeSetView.h"
#import "UtilityConfig.h"

#define MAXROW 16384

@interface TimeSetView ()
<UIPickerViewDataSource,UIPickerViewDelegate>
{
    UIPickerView *timePicker;
    
    UIWindow *window;
    
    NSMutableArray *hours;
    
    NSMutableArray *minutes;
    
}

@end


static TimeSetView *timeSetView;

@implementation TimeSetView

@synthesize selectTimeBlock;

+ (id)sharedTimeSetViewWithTime:(NSString *)timeString withSelectTimeBlock:(SelectTimeBlock)block;
{
    @synchronized (self)
    {
        //为了确保多线程情况下，仍然确保实体的唯一性
        if (timeSetView == nil)
        {
            timeSetView = [[TimeSetView alloc] init];
        }
        
        for (UIView *view in timeSetView.subviews)
        {
            [view removeFromSuperview];
        }
        
        [timeSetView initView];

        timeSetView.hidden = NO;
        
        [timeSetView initBeginTime:timeString];
        
        timeSetView.selectTimeBlock = [block copy];
        
        [WINDOW addSubview:timeSetView];

        return timeSetView;
    }
}


+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if (!timeSetView)
        {
            timeSetView = [super allocWithZone:zone]; //确保使用同一块内存地址
            
            timeSetView.hidden = NO;
            
            return timeSetView;
        }
        
        return nil;
    }
}


- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
    }
    
    return self;
}

- (void)initBeginTime:(NSString *)timeString
{
    NSArray *timeBegins = [timeString componentsSeparatedByString:@":"];
    
    for (int i = 0; i < timeBegins.count; i++)
    {
        NSInteger time = [[NSString stringWithFormat:@"%@",[timeBegins objectAtIndex:i]] integerValue];
        
        NSArray *timeAry = nil;
        
        if (i == 0)
        {
            timeAry = hours;
        }
        else
        {
            timeAry = minutes;
        }
        
        NSUInteger baseCount = (MAXROW/2)-(MAXROW/2)%timeAry.count + time;
        
        [timePicker selectRow:[timePicker selectedRowInComponent:i]%baseCount+baseCount inComponent:i animated:false];
    }
}


- (void)initView
{
    //初始化两列数据
    hours = [[NSMutableArray alloc] init];
    
    minutes = [[NSMutableArray alloc] init];

    for (int i = 0; i < 60; i++)
    {
        if (i < 24)
        {
            [hours addObject:[NSString stringWithFormat:@"%02d",i]];
        }
        [minutes addObject:[NSString stringWithFormat:@"%02d",i]];
    }
    
    
    window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    self.frame = window.bounds;
    [window addSubview:self];
    
    //灰色覆层
    UIImageView *grayImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    grayImageView.backgroundColor = [UIColor colorWithRed:121.f/255.f green:121.f/255.f blue:121.f/255.f alpha:0.7f];
    [self addSubview:grayImageView];

    
    //
    timePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    timePicker.dataSource = self;
    timePicker.delegate = self;
    timePicker.showsSelectionIndicator = YES;
    timePicker.backgroundColor = [UIColor whiteColor];
    [self addSubview:timePicker];
    
    timePicker.center = self.center;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(timePicker.frame.origin.x + 0.5, timePicker.frame.origin.y + timePicker.frame.size.height + 1, timePicker.frame.size.width - 1, 1)];
    imageView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:imageView];
    
    
    //选择时间
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(timePicker.frame.origin.x + 0.5, timePicker.frame.origin.y - 40, timePicker.frame.size.width - 1, 40)];
    titleLabel.text = @"选择时间";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:titleLabel];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(timePicker.frame.origin.x + 0.5, timePicker.frame.origin.y + timePicker.frame.size.height + 1, timePicker.frame.size.width - 1, 40)];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    NSArray *buttonTitles = @[@"确定",@"清除",@"取消"];
    
    for (int i = 0; i < 3; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        [button setTitle:[buttonTitles objectAtIndex:i] forState:UIControlStateNormal];
        button.frame = CGRectMake(i * view.frame.size.width / 3, 0, view.frame.size.width / 3, view.frame.size.height);
        [button setTitleColor:[UIColor colorWithRed:90.0f/255.0f green:170.0f/255.0f blue:250.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        button.tag = i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
        if (i < 2)
        {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(button.frame.origin.x + button.frame.size.width, button.frame.origin.y, 2, 40)];
            imageView.backgroundColor = [UIColor lightGrayColor];
            [view addSubview:imageView];
        }
    }
    
    
}

- (void)buttonClick:(UIButton *)button
{
    NSInteger hourRow = [timePicker selectedRowInComponent:0];
    
    NSInteger minuteRow = [timePicker selectedRowInComponent:1];

    NSString *timeStr = [NSString stringWithFormat:@"%@:%@",[hours objectAtIndex:(hourRow%hours.count)],[minutes objectAtIndex:(minuteRow%minutes.count)]];
    
    NSLog(@"%@", timeStr);
    
    if (selectTimeBlock)
    {
        self.hidden = YES;

        selectTimeBlock((int)button.tag,timeStr);
    }
    
}

#pragma mark - UIPickerViewDataSource - UIPickerViewDelegate  method
#pragma mark -

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return MAXROW;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSArray *timeAry = nil;
    
    if (component == 0)
    {
        timeAry = hours;
    }
    else
    {
        timeAry = minutes;
    }
    
    NSUInteger baseCount = (MAXROW/2)-(MAXROW/2)%timeAry.count;
    
    [pickerView selectRow:[pickerView selectedRowInComponent:component]%baseCount+baseCount inComponent:component animated:false];
}


//替换text居中
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 0.0f, [pickerView rowSizeForComponent:component].width-12, [pickerView rowSizeForComponent:component].height)];

    NSArray *timeAry = nil;
    
    if (component == 0)
    {
        timeAry = hours;
    }
    else
    {
        timeAry = minutes;
    }
    
    label.text = [timeAry objectAtIndex:(row%timeAry.count)];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
}

@end
